<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="姓名" prop="memberName">
        <el-input
          v-model="queryParams.memberName"
          placeholder="请输入姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="性别" prop="memberSex">
        <el-select v-model="queryParams.memberSex" placeholder="请选择性别">
          <el-option
            v-for="item in sex"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="二级学院" prop="collegeId">
        <el-select v-model="queryParams.collegeId" placeholder="请选择二级学院">
          <el-option
            v-for="college in collegeList"
            :key="college.cid"
            :label="college.collegeName"
            :value="college.cid">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="专业班级" prop="memberClass">
        <el-input
          v-model="queryParams.memberClass"
          placeholder="请输入专业班级"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['club:member:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['club:member:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['club:member:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['club:member:export']"
        >导出
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-upload
          class="upload-demo"
          action="http://localhost/dev-api/club/member/import"
          :headers="headers"
          :show-file-list="false" accept="xlsx" :on-success="handleExcelImportSuccess" style="display: inline-block">
          <el-button
            type="primary"
            v-hasPermi="['club:member:import']"
            plain
            size="mini"
            icon="el-icon-upload">导入
          </el-button>
        </el-upload>

      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading" :data="memberList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="id" align="center" prop="id"/>
      <el-table-column label="姓名" align="center" prop="memberName"/>
      <el-table-column label="性别" align="center" prop="memberSex"/>
      <el-table-column label="手机号" align="center" prop="memberPhone"/>
      <el-table-column label="年级" align="center" prop="memberGrade"/>
      <el-table-column label="二级学院" align="center" prop="clubCollege.collegeName"/>
      <el-table-column label="专业班级" align="center" prop="memberClass"/>
      <el-table-column label="职位" align="center" prop="clubPosition.positionName">
        <template scope="scope">
          <el-tag
            :type="scope.row.clubPosition.pid=='1'?''
            :scope.row.clubPosition.pid=='2'?'success':'info'">
            {{ scope.row.clubPosition.positionName }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="加入日期" align="center" prop="createDate"/>
      <el-table-column label="修改日期" align="center" prop="updateDate"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['club:member:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['club:member:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改社团会员对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="姓名" prop="memberName">
          <el-input v-model="form.memberName" placeholder="请输入姓名"/>
        </el-form-item>
        <el-form-item label="性别" prop="memberSex">
          <el-radio v-model="form.memberSex" label="男">男</el-radio>
          <el-radio v-model="form.memberSex" label="女">女</el-radio>
        </el-form-item>
        <el-form-item label="手机号" prop="memberPhone">
          <el-input v-model="form.memberPhone" placeholder="请输入手机号" :maxlength="11" show-word-limit
                    clearable prefix-icon='el-icon-mobile' :style="{width: '100%'}"></el-input>
        </el-form-item>
        <el-form-item label="年级" prop="memberGrade">
          <el-input v-model="form.memberGrade" placeholder="请输入年级，格式:20级" clearable :style="{width: '100%'}">
          </el-input>
        </el-form-item>
        <el-form-item label="二级学院" prop="collegeId">
          <el-select v-model="form.collegeId" placeholder="请选择二级学院">
            <el-option
              v-for="college in collegeList"
              :key="college.cid"
              :label="college.collegeName"
              :value="college.cid">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="专业班级" prop="memberClass">
          <el-input v-model="form.memberClass" placeholder="请输入专业班级"/>
        </el-form-item>
        <el-form-item label="职位" prop="positionId">
          <el-radio-group v-model="form.positionId" size="small">
            <el-radio-button v-for="position in positionIdOptions"
                             :key="position.pid"
                             :label="position.pid"
            >{{ position.positionName }}
            </el-radio-button>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listMember, getMember, delMember, addMember, updateMember} from "@/api/club/member";
import {listCollege} from "@/api/club/college";
import {listPosition} from "@/api/club/position";
import {getToken} from "@/utils/auth";

var token = getToken()
export default {
  name: "Member",
  data() {
    return {
      // token
      headers: {'Authorization': 'Bearer ' + token},
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      //二级学院
      collegeList: [],
      //性别
      sex: [{
        value: '男',
        label: '男'
      }, {
        value: '女',
        label: '女'
      }],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 社团会员表格数据
      memberList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //职位参数
      positionIdOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        memberName: null,
        memberSex: null,
        collegeId: null,
        memberClass: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        memberName: [
          {required: true, message: '请输入姓名', trigger: 'blur'},
        ],
        memberSex: [
          {required: true, message: '请选择性别', trigger: 'blur'}
        ],
        collegeId: [
          {required: true, message: '请选择二级学院', trigger: 'blur'}
        ],
        memberClass: [
          {required: true, message: '请输入专业班级', trigger: 'blur'}
        ],
        memberPhone: [
          {required: true, message: '请输入手机号', trigger: 'blur'},
          {pattern: /^1[34578]\d{9}$/, message: '手机号格式不正确', trigger: 'blur'}
        ],
        memberGrade: [
          {required: true, message: '请输入年级', trigger: 'blur'},
          {min: 3, max: 3, message: '年级格式不正确！', trigger: 'blur'}
        ],
        positionId: [{
          required: true,
          message: '职位不能为空',
          trigger: 'change'
        }]
      }
    };
  },
  created() {
    this.getList();
    this.getCollegeList();
    this.getPositionList();
  },
  methods: {
    //查询职位列表
    getPositionList() {
      listPosition().then(response => {
        console.log("职位-------" + JSON.stringify(response))
        console.log("职位-------" + response)
        this.positionIdOptions = response.rows
      })
    },
    //查询二级学院列表
    getCollegeList() {
      listCollege().then(response => {
        this.collegeList = response.rows;
        console.log(this.collegeList)
        console.log(token)
      })
    },
    /** 查询社团会员列表 */
    getList() {
      this.loading = true;
      listMember(this.queryParams).then(response => {
        this.memberList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        memberName: null,
        memberSex: null,
        collegeId: null,
        memberClass: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加社团会员";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getMember(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改社团会员";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateMember(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMember(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除社团会员编号为"' + ids + '"的数据项？').then(function () {
        return delMember(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('club/member/export', {
        ...this.queryParams
      }, `member_${new Date().getTime()}.xlsx`)
    },
    //导入Excel
    handleExcelImportSuccess() {
      this.$message.success("导入成功")
      this.getList()
    }
  }
};
</script>
